package cn.web.login_servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login_servlet")
public class Login_servlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.设置request编码
        req.setCharacterEncoding("utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String checkCode = req.getParameter("checkCode");

        //3.先获取生成的验证码
        HttpSession session = req.getSession();
        String checkCode_session = (String) session.getAttribute("checkCode_session");

        //删除session中存储的验证码
        session.removeAttribute("checkCode_session");

        //3.先判断验证码是否正确
        if (checkCode_session != null && checkCode_session.equalsIgnoreCase(checkCode)){
            //忽略大小写比较
            //验证码正确
            //判断用户名和密码是否一致
            if ("cpu_code".equals(username) && "123".equals(password)){
                //需要调用UserDao查询数据库

                //登录成功
                //存储信息，用户信息
                session.setAttribute("user", username);

                //重定向到success.jsp
                resp.sendRedirect(req.getContextPath() + "/login_servlet_html/success.jsp");
            } else {
                //登录失败
                //存储提示信息到request
                req.setAttribute("login_error", "用户名或密码错误");

                //转发到登录页面
                req.getRequestDispatcher("/login_servlet_html/login.jsp").forward(req,resp);
            }
        } else {
            //验证码不一致
            //存储提示信息到request
            req.setAttribute("cc_error", "验证码错误");
            //转发到登录页面
            req.getRequestDispatcher("/login_servlet_html/login.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
